package org.utils;

import java.util.ArrayList;
import java.util.Map;
import java.util.Random;
import java.util.UUID;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import com.google.common.collect.Maps;
import com.xiaoleilu.hutool.log.Log;
import com.xiaoleilu.hutool.log.LogFactory;

/**
 * @author : DengYing
 * @CreateDate : 2017年12月8日 下午6:13:17
 * @Description ：常用工具类
 */
public class CommonUtil {
    private final static Log log = LogFactory.get(CommonUtil.class);
	
	public static void changeMap(Map<Integer, Integer> map, int key, int val) {
		Integer oldVal = map.get(key);
		if (oldVal == null) {
			oldVal = val;
			map.put(key, oldVal);
		}else {
			map.put(key, oldVal + val);
		}
	}
	
	public static void changeMap(Map<Integer, Integer> map, Map<Integer, Integer> param) {
		if (MapUtils.isEmpty(param)) {
			return;
		}
		param.forEach((k,v) -> {
			changeMap(map, k,v);
		});
	}

	public static Map<Integer,Integer> packageMap(int key,int value){
		Map<Integer,Integer> map = Maps.newHashMap();
		map.put(key,value);
		return map;
	}
	
	private static String[] chars = new String[]{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D","E", "F", "G", "H","J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y","Z"};

    /**
     * 生成激活码字符串（8位字母及数字组合）
     *
     * @return
     */
    public static String generateShortUuid() {
        StringBuffer shortBuffer = new StringBuffer();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        for (int i = 0; i < 8; i++) {
            String str = uuid.substring(i * 4, i * 4 + 4);
            int x = Integer.parseInt(str, 16);
            shortBuffer.append(chars[x % 34]);
        }
        return shortBuffer.toString();
    }
    
    /**
     * n选1
     * 
     * @param t_select(eg-> "a:10,b:20,c:70")
     * @return
     */
    public static String selectOneOfN(String t_select) {
        String ret = null;
        ArrayList<SelectOdd> selectOdds = new ArrayList<>();
        int total = 0;
        String[] select = StringUtils.split(t_select, ",");
        for (int i = 0; i < select.length; i++) {
            String[] strSelects = StringUtils.split(select[i], ":");
            if (strSelects.length < 2) {
                log.error("配置错误: " + t_select);
                return null;
            }
            String item = strSelects[0];
            int odd = Integer.parseInt(strSelects[1]);
            selectOdds.add(new SelectOdd(item, odd));
            total += odd;
        }
        for (SelectOdd odd : selectOdds) {
            odd.setOddReal((float) odd.getOdd() / (float) total);
        }
        float rand = new Random().nextFloat();
        float oddTmp = 0.0f;
        for (int a = 0; a < selectOdds.size(); a++) {
            SelectOdd dod = selectOdds.get(a);
            oddTmp += dod.getOddReal();
            if (rand <= oddTmp) {
                ret = dod.getItem();
                break;
            }
        }
        return ret;
    }

    /** 抽取概率封装 */
    private static class SelectOdd {
        // 元素
        private String item;
        // 权重
        private int odd;
        // 真实概率
        private float oddReal;

        public SelectOdd(String item, int odd) {
            this.item = item;
            this.odd = odd;
        }

        public String getItem() {
            return item;
        }

        public int getOdd() {
            return odd;
        }

        public float getOddReal() {
            return oddReal;
        }

        public void setOddReal(float oddReal) {
            this.oddReal = oddReal;
        }

    }
}
